package slimeVolleyball;

import java.awt.event.*;
import javax.swing.*;

public class Ball extends JComponent implements ActionListener
{
	int x;
	int initX;
	int y;
	int initY;
	int w;
	int h;
	SlimeGui aGui;
	Timer aTimer;
	Boolean goLeft = true;
	Boolean goRight = false;
	Boolean antiGrav = false;
	Boolean checkContact = true;
	int gravCounter = 1;
	Slime aSlimeOne;
	Slime aSlimeTwo;
	
	int xIncr = 1;
	int yIncr = 1;

	public Ball(int x, int y, int w, int h, SlimeGui aGui, Slime aSlimeOne, Slime aSlimeTwo)
	{
		this.x = x;
		this.initX = x;
		this.y = y;
		this.initY = y;
		this.w = w;
		this.h = h;
		this.aGui = aGui;
		aTimer = new Timer(2, this);
		aTimer.start();
		this.aSlimeOne = aSlimeOne;
		this.aSlimeTwo = aSlimeTwo;
	}

	public void moveBallRight()
	{
		x+=xIncr;
		aGui.update();
	}

	public void moveBallLeft()
	{
		x-=xIncr;
		aGui.update();
	}
	public void gravity()
	{
		y+=yIncr;
	}
	public void antiGravity()
	{
		y-=yIncr;
	}
	public int getX()
	{
		return x;
	}
	public int getY()
	{
		return y;
	}
	public int getW()
	{
		return w;
	}
	public int getH()
	{
		return h;
	}
	public void resetX()
	{
		x = initX; 
	}
	public void resetY()
	{
		y = initY;
	}
	public void reset()
	{
		resetX();
		resetY();
		xIncr = 1;
		aTimer.start();
	}
	
	public int reflectedSlopeOne(int incrX, int x)
	{
		double newX;
		double XMAS = x - aSlimeOne.slimeCoordX(); //90 //xIncr = 1
		
		newX = Math.tan(2 * (Math.atan(-1 / (0.5 * Math.pow(( - (XMAS * XMAS) + (100 * XMAS) + (5000)), -0.5) * (-2 * XMAS + 100))) - Math.atan(xIncr))+Math.atan(xIncr));
		
		return (int) Math.round(1/newX);
	}
	
	public void reflectedSlopeTwo(int incrX, int x)
	{
		double newX;
		
		double BOBMARLEY = incrX; //1
		double XMAS = 90; //90
		
		newX = Math.tan(2 * (Math.atan(-1 / (0.5 * Math.pow(( - (XMAS * XMAS) + (100 * XMAS) + (5000)), -0.5) * (-2 * XMAS + 100))) - Math.atan(BOBMARLEY))+Math.atan(BOBMARLEY));
	}

	public void actionPerformed(ActionEvent e) 
	{
		//checks walls
		if((getX()+getW())>800)
		{
			goRight = false;
			goLeft = true;
		}
		if(getX()<0)
		{
			goRight = true;
			goLeft = false;
		}

		if(goRight)
		{
			moveBallRight();
		}
		else if(goLeft)
		{
			moveBallLeft();
		}
		if((getY() +getHeight()) > 500)
		{
			aTimer.stop();
			reset();
			System.out.println("BOBSFOBSOF");
		}
		if(checkContact)
		{
			int x = getX();
			int xw = getX() + getW();
			int yh = getY() + getH();
			
			
			int sx1 = aSlimeOne.slimeCoordX();
			int sxw1 = aSlimeOne.slimeCoordX()+aSlimeOne.slimeWidth();
			int sw1 = aSlimeOne.slimeWidth();
			int sy1 = aSlimeOne.slimeCoordY();
			int syh1 = aSlimeOne.slimeCoordY()+aSlimeOne.slimeHeight();
			
			int sx2 = aSlimeTwo.slimeCoordX();
			int sxw2 = aSlimeTwo.slimeCoordX()+aSlimeOne.slimeWidth();
			int sw2 = aSlimeTwo.slimeWidth();
			int sy2 = aSlimeTwo.slimeCoordY();
			int syh2 = aSlimeTwo.slimeCoordY()+aSlimeOne.slimeHeight();
			
			if(x<sxw1 && x>sx1 && yh<syh1 && yh>sy1)
			{
				antiGrav = true;
				goRight = false;
				goLeft = true;
				xIncr = reflectedSlopeOne(xIncr, x);
			}
			else if(x<sxw2 && x>sx2 && yh<syh2 && yh>sy2)
			{
				
			}
			
			if(/*xw>sx1 && xw<sx1+sw1/5 && yh>sy1 && yh<syh1 || */ xw>sx2 && xw<sx2+sw2/5 && yh>sy2 && yh<syh2)
			{
				antiGrav = true;
				goRight = false;
				goLeft = true;
				xIncr = 2;
			}
			else if(/*xw>sx1+sw1/5 && xw<sx1+sw1/5*2 && yh>sy1 && yh<syh1 || */ xw>sx2+sw2/5 && xw<sx2+sw2/5*2 && yh>sy2 && yh<syh2)
			{
				antiGrav = true;
				goRight = false;
				goLeft = true;
				xIncr = 1;
			}
			else if(/*xw>sx1+sw1/5*2 && xw<sx1+sw1/5*3 && yh>sy1 && yh<syh1 || */ xw>sx2+sw2/5*2 && xw<sx2+sw2/5*3 && yh>sy2 && yh<syh2)
			{
				antiGrav = true;
				goRight = false;
				goLeft = true;
				xIncr = 0;
			}
			else if(/*xw>sx1+sw1/5*3 && xw<sx1+sw1/5*4 && yh>sy1 && yh<syh1 || */ xw>sx2+sw2/5*3 && xw<sx2+sw2/5*4 && yh>sy2 && yh<syh2)
			{
				antiGrav = true;
				goRight = true;
				goLeft = false;
				xIncr = 1;
			}
			else if(/*xw>sx1+sw1/5*4 && x<sxw1 && yh>sy1 && yh<syh1 || */xw>sx2+sw2/5*4 && x<sxw2 && yh>sy2 && yh<syh2)
			{
				antiGrav = true;
				goRight = true;
				goLeft = false;
				xIncr = 2;
			}
		}
		if(!antiGrav)
		{
			gravity();
		}
		else if(antiGrav)
		{
			if(gravCounter<100)
			{
				antiGravity();
				checkContact = false;
				gravCounter++;
			}
			else
			{
				gravCounter = 1;
				checkContact = true;
				antiGrav = false;
			}
		}
	}
}